package com.pomr.service.message.impl;

import com.pomr.dao.base.BaseDao;
import com.pomr.model.sys.Agency;
import com.pomr.model.sys.MsgContent;
import com.pomr.model.sys.User;
import com.pomr.service.message.MessageService;
import com.pomr.util.page.Page;
import com.pomr.util.page.PageParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @作者：Administrator
 * @时间：13:40 2018/8/28 0028
 * @用途：
 **/
@Service
public class MessageServiceImpl implements MessageService {
    @Autowired
    @Qualifier("baseDaoImpl")
    private BaseDao baseDao;

    @Override
    public Page<List<MsgContent>> searchMsgOfAgency(User user, PageParam param) throws Exception {
        String hql = "select u.agency from User u where u.id = "+user.getId();
        System.out.println(hql);
        List<Agency> agencys = baseDao.findByHql(hql);
        if(agencys!=null && agencys.size()>0){
            hql = "from MsgContent where agency.identifier like '"+agencys.get(0).getIdentifier()+"%' or agency=null order by createTime desc";
        }else{
            hql = "from MsgContent where agency=null order by createTime desc";
        }
        String limit = param.getLimit();
        String page = param.getPage();
        Page<List<MsgContent>> p = new Page<List<MsgContent>>();
        p.setCunrrentPage(Integer.parseInt(page));
        p.setPageSize(Integer.parseInt(limit));
        return baseDao.showPage(hql, "select count(1) "+hql, p);
    }
}
